package com.tanhua.server.service;

import com.tanhua.domain.mongo.*;
import com.tanhua.domain.vo.*;
import com.tanhua.dubbo.api.mongo.QuestionnaireApi;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.naming.ldap.Rdn;
import java.util.*;

@Service
public class TestSoulService {

    @Reference
    private QuestionnaireApi questionnaireApi;

    @Autowired
    private RedisTemplate redisTemplate;

    //查询问卷列表
    public List<Object> queryQuestionnaire() {
        List<Questionnaire> questionnaireList = questionnaireApi.queryQuestionnaire();
        List<Object> list = new ArrayList<>();
        for (Questionnaire questionnaire : questionnaireList) {
            QuestionnaireVo questionnaireVo = new QuestionnaireVo();
            BeanUtils.copyProperties(questionnaire, questionnaireVo);

            List<Questions> questionsList = questionnaireApi.queryQuestions(questionnaire.getId());
            QuestionsVo questionsVo = new QuestionsVo();
            List<Object> questionsVoList = new ArrayList<>();
            for (Questions questions : questionsList) {
                questionsVo.setId(questions.getId().toString());
                questionsVo.setQuestion(questions.getQuestion());
                List<Options> optionsList = questionnaireApi.queryOptions(questions.getId().toString());
                List<Object> optionsVoList = new ArrayList<>();
                for (Options options : optionsList) {
                    OptionsVo optionsVo = new OptionsVo();
                    String id = String.valueOf(options.getId());
                    String option = options.getOption();
                    optionsVo.setId(id);
                    optionsVo.setOption(option);
                    optionsVoList.add(optionsVo);
                }
                questionsVo.setOptions(optionsVoList);
                questionsVoList.add(questionsVo);
                questionnaireVo.setQuestions(questionsVoList);
            }
            //查询是否有报告
            UserReport userReport = questionnaireApi.queryIsLock(UserHolder.getUserId(), questionnaire.getId());
            Questionnaire questionnaireType = null;
            if (userReport != null) {
                questionnaireType = questionnaireApi.queryQuestionnaireType(userReport.getWid());
                String questionnaireName = UserHolder.getUserId() + questionnaireType.getName();
                redisTemplate.opsForValue().set(questionnaireType.getName() + UserHolder.getUserId(), questionnaireName);
            }
            Integer isLock = 1;
            String rid = null;
            if (questionnaire.getName().equals("初级灵魂题")) {
                isLock = 0;
                if (userReport != null) {
                    rid = userReport.getId().toString();
                }
            }
            if (questionnaire.getName().equals("中级灵魂题")) {
                Object questionnaireName = redisTemplate.opsForValue().get("初级灵魂题" + UserHolder.getUserId());
                if (questionnaireName != null) {
                    isLock = 0;
                    if (userReport != null) {
                        rid = userReport.getId().toString();
                    }
                } else {
                    isLock = 1;
                }
            }

            if (questionnaire.getName().equals("高级灵魂题")) {
                Object questionnaireName = redisTemplate.opsForValue().get("中级灵魂题" + UserHolder.getUserId());
                if (questionnaireName != null) {
                    isLock = 0;
                    if (userReport != null) {
                        rid = userReport.getId().toString();
                    }
                } else {
                    isLock = 1;
                }
            }
            questionnaireVo.setReportId(rid);
            questionnaireVo.setIsLock(isLock);

            list.add(questionnaireVo);
        }
        return list;
    }

    //查询得分
    public String queryScore(Map<String, List<Answers>> answers) {
        //map的key
        Set<String> keyList = answers.keySet();
        //答案id
        List<String> optionIdList = new ArrayList<>();
        //问题id
        String questionId = null;
        //问卷类型id
        String questionnaireId = null;
        //获取答案id
        for (String key : keyList) {
            List<Answers> optionList = answers.get(key);
            for (Answers option : optionList) {
                String optionId = option.getOptionId();
                questionId = option.getQuestionId();
                //将答案id存入List中
                optionIdList.add(optionId);
            }
        }
        //根据答案id查询分数,将分数存入list最后计算总分
        List<Integer> scoreList = new ArrayList<>();
        for (String optionId : optionIdList) {
            Integer score = questionnaireApi.queryScore(optionId);
            scoreList.add(score);
        }
        //计算总分
        Integer totalScore = 0;
        for (Integer score : scoreList) {
            totalScore += score;
        }
        //查询答题类型
        if (questionId != null) {
            questionnaireId = questionnaireApi.findQuestionnaireId(questionId);
        }
        //查询报告类型id
        List<String> reportsIdList = questionnaireApi.queryReport();
        String reportId = null;
            //根据总分生成报告
            if (totalScore > 0 && totalScore <= 25) {
                UserReport userReport = questionnaireApi.saveUserReport(UserHolder.getUserId(), questionnaireId, reportsIdList.get(0));
                reportId = String.valueOf(userReport.getId());
            } else if (totalScore > 25 && totalScore <= 50) {
                UserReport userReport = questionnaireApi.saveUserReport(UserHolder.getUserId(), questionnaireId, reportsIdList.get(1));
                reportId = String.valueOf(userReport.getId());
            } else if (totalScore > 50 && totalScore <= 75) {
                UserReport userReport = questionnaireApi.saveUserReport(UserHolder.getUserId(), questionnaireId, reportsIdList.get(2));
                reportId = String.valueOf(userReport.getId());
            } else if (totalScore > 75 && totalScore <= 100) {
                UserReport userReport = questionnaireApi.saveUserReport(UserHolder.getUserId(), questionnaireId, reportsIdList.get(3));
                reportId = String.valueOf(userReport.getId());
            }
        return reportId;
    }

    public ReportVo queryResults(String id) {
        //查询用户报告表获取报告
        UserReport userReport = questionnaireApi.queryUserResults(id);
        String rid = userReport.getRid();
        //根据报告id查询报告
        Report report = questionnaireApi.queryResults(rid);
        String conclusion = report.getConclusion();
        String cover = report.getCover();
        String chouxiang = report.getChouxiang();
        String waixiang = report.getWaixiang();
        String panduan = report.getPanduan();
        String lixing = report.getLixing();
        //封装返回值
        ReportVo reportVo = new ReportVo();
        reportVo.setConclusion(conclusion);
        reportVo.setCover(cover);
        String key = "外向,判断,抽象,理性";
        String value = waixiang + "," + panduan + "," + chouxiang + "," + lixing;

        String[] keyArray = key.split(",");
        String[] valueArray = value.split(",");
        List<Object> list = new ArrayList<>();
        for (int i = 0; i < keyArray.length; i++) {
            Dimensions dimensions = new Dimensions();
            dimensions.setKey(keyArray[i]);
            dimensions.setValue(valueArray[i]);
            list.add(dimensions);
        }
        reportVo.setDimensions(list);
        //返回结果
        return reportVo;
    }
}